package com.cgw.game.ui;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.cgw.game.R;
import com.cgw.game.view.MyHorizontalBarChart;
import com.cgw.game.view.MyYAxisRenderer;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;

import java.util.ArrayList;
import java.util.List;

public class LineChartActivity extends AppCompatActivity implements View.OnClickListener{
    MyHorizontalBarChart chart;
    ArrayList<BarEntry> all = new ArrayList<>();
    private String[] titles = new String[]{"周一","周二","周三","周四","周五","周六","周日",};

    private String[] titleY = new String[]{"18:00", "19:00", "20:00", "21:00", "22:00", "23:00",
            "00:00", "01:00", "02:00", "03:00", "04:00", "05:00",
            "06:00", "07:00", "08:00", "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00",
            "18:00", "18:00", "19:00", "20:00", "21:00", "22:00", "23:00",
            "00:00", "01:00", "02:00", "03:00", "04:00", "05:00",
            "06:00", "07:00", "08:00", "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00",
            "18:00"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_line_chart);
        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle(getIntent().getStringExtra("title"));
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//左侧添加一个默认的返回图标
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();//返回
            }
        });
        chart = findViewById(R.id.chart);
        initLine();
        findViewById(R.id.add_all).setOnClickListener(this);
    }
    private void initStepLine(List<BarEntry>  values) {
        XAxis xAxis = chart.getXAxis();
        xAxis.setValueFormatter(new IAxisValueFormatter() { // 转换要显示的标签文本，value值默认是int从0开始
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return titles[(int)value];
            }
        });
        lineData(values);
    }
    private void initLine() {

        chart.setExtraBottomOffset(5f);
//        chart3Line.getAxisLeft().setDrawGridLines(false);
        chart.setTouchEnabled(true);
        chart.setDoubleTapToZoomEnabled(false);
        chart.setScaleEnabled(false);
        chart.setPinchZoom(true);
        //取消说明
        chart.getLegend().setEnabled(false);
        chart.getDescription().setEnabled(false);

        chart.setNoDataText("暂无数据");

        //线条平滑
//        setStartAtZero
        //竖向轴线设置
        chart.getAxisLeft().setEnabled(false);
        chart.setRendererRightYAxis(new MyYAxisRenderer(chart.getViewPortHandler(),chart.getAxisRight(),chart.getTransformer(YAxis.AxisDependency.RIGHT)));
        YAxis rightAxis = chart.getAxisRight();
        rightAxis.removeAllLimitLines(); // reset all limit lines to avoid overlapping lines
        rightAxis.setTextSize(8f);
        rightAxis.setTextColor(Color.WHITE);
//      leftAxis.setValueFormatter(new PercentFormatter());
       /* rightAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {

                return titleY[(int)value];
            }
        });*/
        rightAxis.setDrawAxisLine(true);
        rightAxis.setAxisLineColor(Color.WHITE);
        rightAxis.setDrawGridLines(false);
        LimitLine limitLine1 = new LimitLine(3,"");
        limitLine1.setLineWidth(30f);

        limitLine1.setLineColor(getResources().getColor(R.color.gray));
        LimitLine limitLine2 = new LimitLine(12,"");
        limitLine2.setLineWidth(30f);
        limitLine2.setLineColor(getResources().getColor(R.color.gray));
        rightAxis.addLimitLine(limitLine1);
        rightAxis.addLimitLine(limitLine2);
        rightAxis.setAxisMaximum(26f);
//        rightAxis.setAxisMinimum(0.5f);
        rightAxis.setLabelCount(7,false);


        //横向轴线设置
        XAxis xAxis = chart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setAxisMinimum(-0.5f);
        xAxis.setTextSize(12);
        xAxis.setTextColor(getResources().getColor(R.color.white));
        xAxis.setEnabled(true);
        xAxis.setDrawGridLines(false);
        xAxis.setGranularity(1f);
        xAxis.setAxisLineColor(Color.TRANSPARENT);
        //设置为虚线
        xAxis.setDrawAxisLine(true);
    }
    private void lineData(List<BarEntry> barEntries) {
        //初始化线形图的数据
        BarDataSet barDataSet;
        chart.animateY(1000);
        if (chart.getData() != null &&
                chart.getData().getDataSetCount() > 0) {
            barDataSet = (BarDataSet) chart.getData().getDataSetByIndex(0);
            chart.getData().setBarWidth(0.3f);
            barDataSet.setValues(barEntries);
            chart.getData().notifyDataChanged();
            chart.notifyDataSetChanged();
        } else {
            barDataSet = new BarDataSet(barEntries, "LAR");
            barDataSet.setColor(getResources().getColor(R.color.colorAccent)); // 设置柱形图颜色
            barDataSet.setColors(Color.TRANSPARENT,getResources().getColor(R.color.gray1)); // 设置柱形图颜色
            barDataSet.setHighLightColor(getResources().getColor(R.color.white));
            barDataSet.setHighLightAlpha(255);
            barDataSet.setDrawValues(true);
            BarData barData = new BarData();
            barData.addDataSet(barDataSet);// 添加一组柱形图，如果有多组柱形图数据，则可以多次addDataSet来设置
            barData.setBarWidth(0.3f);
            chart.setData(barData);
            chart.notifyDataSetChanged();
        }
        chart.setFitBars(true);
        chart.invalidate();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.add_all:
                all.add(new BarEntry(0,new float[]{0,24}));
                all.add(new BarEntry(1,new float[]{9,5,2,7}));
                all.add(new BarEntry(2,new float[]{0,8,5}));
                all.add(new BarEntry(3,new float[]{0,8,5}));
                all.add(new BarEntry(4,new float[]{0,8,5}));
                all.add(new BarEntry(5,new float[]{0,8,5}));
                all.add(new BarEntry(6,new float[]{22,2}));
                initStepLine(all);
                break;
        }
    }
}
